<template>
    <el-dialog title="确认提车" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="auto">
            <c-form-template>
                <el-col :span="19" :offset="2">
                    <el-form-item label="提车方式：">
                        <el-input :value="ruleForm.pickupMode" disabled />
                    </el-form-item>
                </el-col>
                <el-col :span="19" :offset="2">
                    <el-form-item label="提车地点：" prop="pickupAddress">
                        <el-select v-model="ruleForm.pickupAddress" @change="pickupAddressChange" :disabled="ruleForm.isView" filterable allow-create default-first-option placeholder="请选择或输入" style="display: block">
                            <el-option v-for="item in wareList" :key="item.id" :label="item.name" :value="item.name">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="19" :offset="2">
                    <el-form-item label="联系人：" prop="contactName">
                        <el-input v-model="ruleForm.contactName" :disabled="ruleForm.isView" />
                    </el-form-item>
                </el-col>
                <el-col :span="19" :offset="2">
                    <el-form-item label="联系方式：" prop="contactMobile">
                        <el-input v-model="ruleForm.contactMobile" :disabled="ruleForm.isView" />
                    </el-form-item>
                </el-col>

                <el-col :span="19" :offset="2" v-if="ruleForm.isView && ruleForm.pickupProofUrl">
                    <el-form-item label="提车凭证：">
                        <el-link type="primary" @click="handleOpenFile(ruleForm.pickupProofUrl)">查看</el-link>
                    </el-form-item>
                </el-col>
            </c-form-template>
        </el-form>

        <span slot="footer" v-if="!ruleForm.isView">
            <el-button :loading="loading" @click="handleClose">取 消</el-button>
            <el-button :loading="loading" type="primary" @click="handleSubmit">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
export default {
    data () {
        return {
            dialogVisible: false,
            loading: false,
            isContactName: false,
            ss: "",
            ruleForm: {
                contactName: "",
                contactMobile: ""
            },
            rules: {
                contactName: [
                    { required: true, message: "请输入联系人", trigger: "blur" },
                ],
                contactMobile: [
                    { required: true, message: "请输入联系人联系方式", trigger: "blur" },
                ],
                pickupAddress: [
                    { required: true, message: "请选择或输入提车地点", trigger: "blur" },
                ],
                timeOut: [
                    { required: true, message: "请选择出库时间", trigger: "blur" },
                ],
            },
            wareList: [],
        };
    },
    methods: {
        pickupAddressChange () {
            let list = []
            this.wareList.forEach((item) => {
                list.push(item.name)
            })
            let index = list.indexOf(this.ruleForm.pickupAddress)
            if (index != -1) {
                this.ruleForm.contactName = this.wareList[index].contactPerson;
                this.ruleForm.contactMobile = this.wareList[index].contactPhone
            }
        },
        handleOpenFile (filePath) {
            if (filePath) {
                window.open(filePath);
            } else {
                this.$message.warning("服务器地址不存在");
            }
        },
        async getWarehouse () {
            this.wareList = await this.$post({ url: "/StoreInfo/list" });
            // this.pickupAddressChange()
        },
        handleSubmit () {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.$confirm("是否确认此操作?", "提示", {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning",
                    }).then(() => {
                        this.loading = true;
                        this.$post({
                            url: "/buyerPickupApply/confirm/platform",
                            data: {
                                id: this.ruleForm.orderId,
                                pickupAddress: this.ruleForm.pickupAddress,
                                timeOut: this.ruleForm.timeOut,
                                contactName: this.ruleForm.contactName,
                                contactMobile: this.ruleForm.contactMobile,
                            },
                        }).then(
                            () => {
                                this.$message.success("操作成功");
                                this.handleClose();
                                this.$parent.$parent.tableList();
                                this.loading = false;
                            },
                            () => {
                                this.loading = false;
                            }
                        );
                    });
                }
            });
        },
        handleOpen (item) {
            if (item && item.id) {
                this.ruleForm = {
                    ...item,
                };
            }
            if (!item.isView) {
                this.getWarehouse();
            }
            this.dialogVisible = true;
            this.loading = false;
        },
        handleClose () {
            this.ruleForm = {
                address: [],
            };
            this.$refs.ruleForm.resetFields();
            this.dialogVisible = false;
        },
    },
};
</script>
